package com.lee.account.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.lee.account.entity.SysUser;
import com.lee.account.service.SysUserService;
import com.lee.account.utils.MD5;
import com.lee.account.utils.R;
import com.lee.account.utils.StringUtil;

/**
 * 用户Controller
 * @author liyou
 * @date 2019年9月3日 下午4:01:35
 */
@RestController
@RequestMapping("/app/user")
public class SysUserController {

	public static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private  SysUserService sysUserService;
	
	@GetMapping("/list")
	public R list(int page,int limit) {
		try {
			Page<SysUser> userPage = new Page<SysUser>(page,limit);
			sysUserService.selectPage(userPage);
    		return R.success().put("data", userPage.getRecords()).put("count", userPage.getTotal()).put("code", "0");
		} catch (Exception e) {
			logger.error("查询用户列表异常：", e);
			return R.serverError();
		}
	}
	
	@GetMapping("/get/{id}")
	public R get(@PathVariable String id) {
		try {
			if(StringUtils.isBlank(id)) {
				return R.paramsError();
			}
			
			return R.success().put("user", sysUserService.selectById(id));
		} catch (Exception e) {
			logger.error("查询用户信息异常：", e);
			return R.serverError();
		}
	}
	
	@PostMapping("/delete/{ids}")
	public R delete(@PathVariable String ids) {
		try {
			if(StringUtils.isBlank(ids)) {
				return R.paramsError();
			}
			String[] idArray = ids.split(",");
			List<String> idList = new ArrayList<String>(idArray.length);
			Collections.addAll(idList,idArray);
			
			if(sysUserService.deleteBatchIds(idList))
				return R.success("删除用户成功");
			else
				return R.error("删除用户失败");
		} catch (Exception e) {
			logger.error("删除用户异常：", e);
			return R.serverError();
		}
	}
	
	@PostMapping("/updateOrinsert")
	public R updateOrinsert(SysUser user) {
		try {
			if(user == null) {
				return R.paramsError();
			}
			
			if(StringUtils.isBlank(user.getId())) {
				user.setPassword(MD5.md5(user.getPassword()));
				user.setId(StringUtil.getUUID());
				if(sysUserService.insert(user))
					return R.success("添加用户成功");
				else
					return R.error("添加用户失败");
			}else {
				if(sysUserService.updateById(user))
					return R.success("修改用户成功");
				else
					return R.error("修改用户失败");
			}
			
			
		} catch (Exception e) {
			logger.error("操作用户异常：", e);
			return R.serverError();
		}
	}
	
}
